#pragma once

#include "Core.h"
#include "Item.h"

class GroupItem : public Item
{
	Q_OBJECT

public:
	GroupItem(Core * aCore, const QString & aID)
		: Item(aID),
		  mCore(aCore)
	{
	}

	/// Return unique id of this item.
	virtual QUuid getID()
	{
		return "02165B7E-D6E8-4083-BDD2-05DEC633176C";
	}

	/// Build current item.
	virtual bool build()
	{
		emit stateChanged(Building);
		
		mCurrentItem = mSubItems.begin();

		buildNextItem();
	}

protected slots:
	virtual void onSubItemStateChanged(Item::State aState)
	{
		if (aState == Complete)
		{
			mCurrentItem++;

			buildNextItem();
		}
		else if (aState == Error)
		{
			emit stateChanged(Error);
		}
	}

protected:
	void buildNextItem()
	{
		if (mCurrentItem != mSubItems.end())
		{
			(*mCurrentItem)->build();
		}
		else
		{
			stateChanged(Complete);
		}
	}

private:
	Core * mCore;
	TSubItems mSubItems;

	TSubItems::iterator mCurrentItem;
};